package com.bawei.rk_01.fm

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import com.bawei.base.utils.mvvm.BaseFragment
import com.bawei.rk_01.Adapter.GoodsAdapter
import com.bawei.rk_01.Adapter.OneAdapter
import com.bawei.rk_01.Adapter.TwoAdapter
import com.bawei.rk_01.R
import com.bawei.rk_01.cartgory.CategoryViewModel
import com.bawei.rk_01.databinding.FragmentCategoryBinding
import com.blankj.utilcode.util.ToastUtils
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener

class CategoryFragment : BaseFragment<FragmentCategoryBinding,CategoryViewModel>() {

    lateinit var oneAdapter: OneAdapter
    lateinit var twoAdapter: TwoAdapter
    lateinit var goodsAdapter: GoodsAdapter

    var page = 1
    var cid = 0

    override fun getViewModel(): Class<CategoryViewModel> {
        return CategoryViewModel::class.java
    }

    override fun getLayout(): Int {
        return R.layout.fragment_category
    }

    override fun initView() {

        //分类
        oneAdapter = OneAdapter()
        binding.rvOne.layoutManager = LinearLayoutManager(context)
        binding.rvOne.adapter = oneAdapter

        twoAdapter = TwoAdapter()
        binding.rvTwo.layoutManager = GridLayoutManager(context,4)
        binding.rvTwo.adapter = twoAdapter

        viewModel.category()

        //点击一级分类，二级分类可变化 三级分类发送变化
        oneAdapter.setOnItemClickListener { adapter, view, position ->
            oneAdapter.IndexId(oneAdapter.data.get(position).id)
            twoAdapter.data.clear()
            twoAdapter.data.addAll(oneAdapter.data.get(position).child)
            twoAdapter.notifyDataSetChanged()
            //当二级数据未被点击时，默认展示第一个子集
            viewModel.goods(twoAdapter.data.get(0).id,"",5,false,1,"","")
            //给全局变量进行赋值  加载刷新不会出现数据混乱情况
            page = 1
            cid = twoAdapter.data.get(0).id
        }

        //点击二级分类，三级数据发生变化
        twoAdapter.setOnItemClickListener { adapter, view, position ->
            viewModel.goods(twoAdapter.data.get(position).id,"",5,false,1,"","")
            page = 1
            cid = twoAdapter.data.get(position).id
        }

        //商品
        goodsAdapter = GoodsAdapter()
        binding.rvGoods.layoutManager = GridLayoutManager(context,2)
        binding.rvGoods.adapter = goodsAdapter

        //下拉加载，上拉刷新
        binding.sm.setOnRefreshLoadMoreListener(object : OnRefreshLoadMoreListener {
            override fun onRefresh(refreshLayout: RefreshLayout) {
                viewModel.goods(cid,"",5,false,1,"","")
                binding.sm.finishRefresh()
            }

            override fun onLoadMore(refreshLayout: RefreshLayout) {
                viewModel.goods(cid,"",5,false,1,"","")
                binding.sm.finishLoadMore()
            }
        })

    }

    override fun initData() {

        //解析
        viewModel.categorysuccess.observe(this){
            if (it.code == 200){

                //解析一级分类
                oneAdapter.data.clear()
                oneAdapter.data.addAll(it.data)
                oneAdapter.notifyDataSetChanged()
                //解析二级分类
                twoAdapter.data.clear()
                twoAdapter.data.addAll(oneAdapter.data.get(0).child) // 没有点击时，默认展示第一个子集
                twoAdapter.notifyDataSetChanged()

                //三级分类商品列表
                viewModel.goods(twoAdapter.data.get(0).id,"",5,false,1,"","")

            }else{
                ToastUtils.showLong(it.message)
            }
        }

        //商品展示
        viewModel.goodssuccess.observe(this){
            if (it.code == 200){
                //是否是初始化状态 是否是刷新状态
                if (page == 1){
                    goodsAdapter.data.clear()
                }
                goodsAdapter.data.addAll(it.data.list)
                goodsAdapter.notifyDataSetChanged()
                if (it.data.list.isEmpty()){
                    ToastUtils.showLong("没有更多数据")
                }
            }else{
                ToastUtils.showLong(it.message)
            }
        }

    }

}